package com.hanyc.spring.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanyc.spring.result.Result;

import com.hanyc.spring.dto.Demo3DTO;
import com.hanyc.spring.vo.Demo3VO;
import com.hanyc.spring.entity.Demo3;
import com.hanyc.spring.util.QueryUtils;
import com.hanyc.spring.service.IDemo3Service;
import com.hanyc.spring.util.MapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;



import java.util.Arrays;
import java.util.List;


/**
 * 【DEMO_3】控制器层
 *
 * @author hanyc
 * @date 2025-06-13
 */
@Slf4j
@Api(tags = {"【DEMO_3】模块Controller"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/demo3")
public class Demo3Controller {

    private final IDemo3Service  demo3Service;

    @ApiOperation(value = "DEMO_3列表查询")
    @PostMapping("/list")
    public Result<List<Demo3VO>> list(@RequestBody Demo3DTO dto) {
        return Result.ok(MapperUtils.INSTANCE.mapAsList(Demo3VO.class,demo3Service.list(Wrappers.lambdaQuery(BeanUtil.copyProperties(dto, Demo3.class)))));
    }

    @ApiOperation(value = "DEMO_3分页查询")
    @PostMapping("/page")
    public Result<Page<Demo3VO>> page(@RequestBody Demo3DTO dto) {
        Page<Demo3> page = new Page<>(dto.getCurrent(), dto.getSize());
        Page<Demo3> result = demo3Service.page(page, QueryUtils.dynamicSort(dto.getSortBy(), Wrappers.query(BeanUtil.copyProperties(dto, Demo3.class))));
        return Result.ok(MapperUtils.INSTANCE.mapAsPage(Demo3VO.class, result));
    }

    @ApiOperation(value = "DEMO_3添加,并获取ID")
    @PostMapping("/insertReturnId")
    public Result<Long> insertReturnId(@RequestBody Demo3DTO dto) {
        Demo3 entity = BeanUtil.copyProperties(dto, Demo3.class);
        demo3Service.save(entity);
        return Result.ok(entity.getId());
    }

    @ApiOperation(value = "DEMO_3添加")
    @PostMapping
    public Result<Boolean> insert(@RequestBody Demo3DTO dto) {
        Demo3 entity = BeanUtil.copyProperties(dto, Demo3.class);
        return Result.ok(demo3Service.save(entity));
    }

    @ApiOperation(value = "DEMO_3更新")
    @PutMapping
    public Result<Boolean> update(@RequestBody Demo3DTO dto) {
        Demo3 entity = BeanUtil.copyProperties(dto, Demo3.class);
        return Result.ok(demo3Service.updateById(entity));
    }

    @ApiOperation(value = "DEMO_3删除")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.ok(demo3Service.removeById(id));
    }

    @ApiOperation(value = "DEMO_3详情")
    @GetMapping("/{id}")
    public Result<Demo3VO> detail(@PathVariable("id") Long id) {
        return Result.ok(BeanUtil.copyProperties(demo3Service.getById(id), Demo3VO.class));
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids){
        return Result.ok(demo3Service.removeByIds(ids));
    }


}

